package com.salton123.groupbargain.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.viewpager.widget.PagerAdapter;

import com.salton123.groupbargain.R;
import com.salton123.groupbargain.model.BannerEntity;
import com.salton123.utils.ImageLoader;

import java.util.List;

/**
 * User: newSalton@outlook.com
 * Date: 2019/9/26 10:44
 * ModifyTime: 10:44
 * Description:
 */
public class UltraPagerAdapter extends PagerAdapter {
    List<BannerEntity> mDatas;

    public UltraPagerAdapter(List<BannerEntity> datas) {
        this.mDatas = datas;
    }

    public interface ImageClick {
        void onClick(int position, BannerEntity item);
    }

    public void setImageClick(ImageClick imageClick) {
        mImageClick = imageClick;
    }

    private ImageClick mImageClick;

    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ViewGroup viewGroup = (ViewGroup) LayoutInflater.from(container.getContext()).inflate(R.layout.banner_item, null);
        ImageView imageView = viewGroup.findViewById(R.id.bannerImage);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mImageClick != null) {
                    mImageClick.onClick(position, mDatas.get(position));
                }
            }
        });
        ImageLoader.loadCenterCrop(imageView, mDatas.get(position).getImageResId());
        container.addView(viewGroup);
        return viewGroup;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        container.removeView(view);
    }
}